﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControlsLib.Toolbars
{
    public class MainFormToolbar : ToolStrip
    {
        public event EventHandler<CustomEventArgs<object>> EventRaised;
        private Dictionary<string, bool> m_eventManagement = null;

        public Dictionary<string, bool> EventManagement
        {
            get { return m_eventManagement; }
            set { m_eventManagement = value; }
        }

        public MainFormToolbar()
        {
            this.Name = "MainToolbar";

            m_eventManagement = new Dictionary<string, bool>();
        }

        public MainFormToolbar(string toolbarName)
        {
            this.Name = toolbarName;
        }

        //public void AddItem<T>(Action action, T item) where T : ToolStripItem
        //{
        //    action();

        //    m_eventManagement.Add(item, false, item);

        //    StartEventManagement();
        //}

        public void AddItem<T>(Action action, string item, string handler)
        {
            action();

            m_eventManagement.Add(item, false);

            StartEventManagement<T>(handler);
        }

        public ToolStripComboBox CreaCombo(string name, string caption)
        {
            ToolStripComboBox cmb = new ToolStripComboBox(name);
            cmb.AutoSize = true;
            cmb.Alignment = ToolStripItemAlignment.Right;
            ToolStripLabel lbl = new ToolStripLabel(string.Format("{0}: ", caption));
            lbl.Alignment = ToolStripItemAlignment.Right;
            this.Items.Add(cmb);
            this.Items.Add(lbl);
            return cmb;
        }

        public T GetSelectedItem<T>(string combo)
        {
            return (T)((ToolStripComboBox)this.Items[combo]).ComboBox.SelectedItem;
        }

        public void SetSelectedItem<T>(string combo, string value)
        {
            ComboBox cmb = ((ToolStripComboBox)this.Items[combo]).ComboBox;
            FrameworkLib.Utilities.ComboBoxHelper.SelectComboItem(cmb, value);
            
            //((ToolStripComboBox)this.Items[combo]).ComboBox.SelectedItem = value;
        }

        public void Enabled(string combo, bool enabled)
        {
            ((ToolStripComboBox)this.Items[combo]).ComboBox.Enabled = enabled;
        }

        public ToolStripItem GetToolbarItem(string name)
        {
            return this.Items[name];
        }

        public void OnEventRaised(object sender)
        {
            if (EventRaised != null)
            {
                EventRaised(this, new CustomEventArgs<object>(Actions.FromCombo, sender));
            }
        }

        void cmb_SelectedIndexChanged(object sender, EventArgs e)
        {
            //m_toolbar.OnEventRaised(sender);
        }

        private void StartEventManagement<T>(string eventName)
        {
            foreach (bool b in m_eventManagement.Values)
            {
                if (!b) return;
            }

            var evt = typeof(T).GetEvent(eventName);

            Type argsType = getEventArgsType(evt);

            System.Reflection.MethodInfo hook = evt.GetAddMethod();
            //System.Reflection.MethodInfo boundEventhandler = hook.m.MakeGenericMethod(new[] { argsType });

            Delegate handler = Delegate.CreateDelegate(evt.EventHandlerType, hook);

            evt.AddEventHandler(this, handler);

            //cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
            //cmb1.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
            //cmb2.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
        }

        private Type getEventArgsType(System.Reflection.EventInfo eventType)
        {
            Type t = eventType.EventHandlerType;
            System.Reflection.MethodInfo m = t.GetMethod("Invoke");

            var parameters = m.GetParameters();
            return parameters[1].ParameterType;
        }

        public void RefreshToolbar<T>(string eventName)
        {
            base.Refresh();

            StartEventManagement<T>(eventName);
        }
    }
}
